package Controller;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import Jdbc.JdbcSedat;
import Model.SistemYoneticisi;
import Model.Uye;

import com.opensymphony.xwork2.ActionSupport;

public class ControllerSedat extends ActionSupport{

	private InputStream inputStream;
	private int sayfa;
	private String sonuc;
	private String basvuru_pk;
	private String rol;
	private String uye_pk;
	private String editor_pk;
	private String karar;
	private String dergi_ismi;
	private String dergi_pk;
	private String mesaj_pk;
	private String konu_pk;
	private String bilim_dali_pk;
	private String sayi_pk;
	private String makale_pk;
	private String rol_no;

 
	public String rolBasvurulari() throws UnsupportedEncodingException{
		sonuc=((SistemYoneticisi)getSession("admin")).rolBasvuruListele();
		inputStream = new ByteArrayInputStream(sonuc.getBytes("UTF-8"));
		return SUCCESS;
	}
	
	public String uyeSessionKontrol() throws UnsupportedEncodingException{
		if(getSession("uye")==null){
			sonuc="yetkisiz";
		}
		else{
			sonuc="yetkili";
		}
		inputStream = new ByteArrayInputStream(sonuc.getBytes("UTF-8"));
		return SUCCESS;
	}
	
	public String sessionKontrol() throws UnsupportedEncodingException{
		if(getSession("uye")!=null){
			sonuc="uye_acik";
		}
		else if(getSession("admin")!=null){
			sonuc="admin";
		}
		else{
			sonuc="yetkisiz";
		}
		inputStream = new ByteArrayInputStream(sonuc.getBytes("UTF-8"));
		return SUCCESS;
	}
	
	public String adminSessionKontrol() throws UnsupportedEncodingException{
		if(getSession("admin")==null){
			if(getSession("uye")==null){
				sonuc="yetkisiz";
			}
			else{
				sonuc="uye_acik";
			}
			
		}
		else{
			sonuc="yetkili";
		}
		inputStream = new ByteArrayInputStream(sonuc.getBytes("UTF-8"));
		return SUCCESS;
	}
	
	public String yazarSessionKontrol() throws UnsupportedEncodingException{
		Uye uye=(Uye)getSession("uye");
		if(uye==null){
			sonuc="yetkisiz";
		}
		else{
			if(uye.getAktifRol()[0]==null){
				sonuc="uye_acik";
			}
			else{
				sonuc="yetkili";
			}
		}
		inputStream = new ByteArrayInputStream(sonuc.getBytes("UTF-8"));
		return SUCCESS;
	}
	
	public String hakemSessionKontrol() throws UnsupportedEncodingException{
		Uye uye=(Uye)getSession("uye");
		if(uye==null){
			sonuc="yetkisiz";
		}
		else{
			if(uye.getAktifRol()[1]==null){
				sonuc="uye_acik";
			}
			else{
				sonuc="yetkili";
			}
		}
		inputStream = new ByteArrayInputStream(sonuc.getBytes("UTF-8"));
		return SUCCESS;
	}
	
	public String editorSessionKontrol() throws UnsupportedEncodingException{
		Uye uye=(Uye)getSession("uye");
		if(uye==null){
			sonuc="yetkisiz";
		}
		else{
			if(uye.getAktifRol()[2]==null){
				sonuc="uye_acik";
			}
			else{
				sonuc="yetkili";
			}
		}
		inputStream = new ByteArrayInputStream(sonuc.getBytes("UTF-8"));
		return SUCCESS;
	}
	
	public String rolKontrolEt() throws UnsupportedEncodingException{
		Uye uye=(Uye)getSession("uye");
		sonuc="yetkisiz";
		if(rol_no.equals("1")){
			if(uye.getAktifRol()[0]!=null){
				sonuc="yazar";
			}
		}
		else if(rol_no.equals("2")){
			if(uye.getAktifRol()[2]!=null){
				sonuc="editor";
			}
		}
		else if(rol_no.equals("3")){
			if(uye.getAktifRol()[1]!=null){
				sonuc="hakem";
			}
		}
		inputStream = new ByteArrayInputStream(sonuc.getBytes("UTF-8"));
		return SUCCESS;
	}
	
	public void rolKarari() throws UnsupportedEncodingException{
		((SistemYoneticisi)getSession("admin")).rolKarariVer(basvuru_pk,rol,uye_pk,karar);
	}
	
	public void dergiKarari() throws UnsupportedEncodingException{
		((SistemYoneticisi)getSession("admin")).dergiKarariVer(basvuru_pk,editor_pk,uye_pk,dergi_ismi,karar);
	}
	
	public void yetkiKarari() throws UnsupportedEncodingException{
		((SistemYoneticisi)getSession("admin")).yetkiKarariVer(basvuru_pk,editor_pk,uye_pk,dergi_pk,dergi_ismi,karar);
	}
	
	public void silinmeKarari() throws UnsupportedEncodingException{
		((SistemYoneticisi)getSession("admin")).silinmeKarariVer(uye_pk,karar);
	}
	
	public void mesajOkundu() throws UnsupportedEncodingException{
		((Uye)getSession("uye")).mesajOkundu(mesaj_pk);
	}
	
	public String basvuruyaUygunRol() throws UnsupportedEncodingException{
		sonuc=JdbcSedat.basvuruyaUygunRol(((Uye)getSession("uye")).getUye_id());
		inputStream = new ByteArrayInputStream(sonuc.getBytes("UTF-8"));
		return SUCCESS;
	}
	
	public String dergiBasvurulari() throws UnsupportedEncodingException{
		sonuc=((SistemYoneticisi)getSession("admin")).dergiBasvuruListele();
		inputStream = new ByteArrayInputStream(sonuc.getBytes("UTF-8"));
		return SUCCESS;
	}
	
	public String tumDergileriListele() throws UnsupportedEncodingException{
		sonuc=JdbcSedat.tumDergileriListele(konu_pk,bilim_dali_pk);
		inputStream = new ByteArrayInputStream(sonuc.getBytes("UTF-8"));
		return SUCCESS;
	}
	
	public String yetkiBasvurulari() throws UnsupportedEncodingException{
		sonuc=((SistemYoneticisi)getSession("admin")).yetkiBasvuruListele();
		inputStream = new ByteArrayInputStream(sonuc.getBytes("UTF-8"));
		return SUCCESS;
	}
	
	
	public String dergiSayiListele() throws UnsupportedEncodingException{
		sonuc=JdbcSedat.dergiSayiListele(dergi_pk);
		inputStream = new ByteArrayInputStream(sonuc.getBytes("UTF-8"));
		return SUCCESS;
	}
	
	public String silinmeBasvurulari() throws UnsupportedEncodingException{
		sonuc=((SistemYoneticisi)getSession("admin")).silinmeBasvuruListele();
		inputStream = new ByteArrayInputStream(sonuc.getBytes("UTF-8"));
		return SUCCESS;
	}
	
	public String uyeListele() throws UnsupportedEncodingException{
		sonuc=((SistemYoneticisi)getSession("admin")).uyeListele();
		inputStream = new ByteArrayInputStream(sonuc.getBytes("UTF-8"));
		return SUCCESS;
	}
	
	public String dergiYetkiListele() throws UnsupportedEncodingException{
		sonuc=((SistemYoneticisi)getSession("admin")).dergiYetkiListele();
		inputStream = new ByteArrayInputStream(sonuc.getBytes("UTF-8"));
		return SUCCESS;
	}
	
	public String sayiMakaleListele() throws UnsupportedEncodingException{
		sonuc=JdbcSedat.sayiMakaleListele(sayi_pk);
		inputStream = new ByteArrayInputStream(sonuc.getBytes("UTF-8"));
		return SUCCESS;
	}
	
	public String makaleYorumGetir() throws UnsupportedEncodingException{
		sonuc=JdbcSedat.makaleYorumGetir(makale_pk);
		inputStream = new ByteArrayInputStream(sonuc.getBytes("UTF-8"));
		return SUCCESS;
	}
	
	public String gelenMesajListele() throws UnsupportedEncodingException{
		Uye uye=(Uye)getSession("uye");
		sonuc=uye.gelenMesajListele();
		inputStream = new ByteArrayInputStream(sonuc.getBytes("UTF-8"));
		return SUCCESS;
	}
	
	
	

	public String getRol_no() {
		return rol_no;
	}

	public void setRol_no(String rol_no) {
		this.rol_no = rol_no;
	}

	public String getMakale_pk() {
		return makale_pk;
	}

	public void setMakale_pk(String makale_pk) {
		this.makale_pk = makale_pk;
	}

	public String getSayi_pk() {
		return sayi_pk;
	}

	public void setSayi_pk(String sayi_pk) {
		this.sayi_pk = sayi_pk;
	}

	public String getKonu_pk() {
		return konu_pk;
	}

	public void setKonu_pk(String konu_pk) {
		this.konu_pk = konu_pk;
	}

	public String getBilim_dali_pk() {
		return bilim_dali_pk;
	}

	public void setBilim_dali_pk(String bilim_dali_pk) {
		this.bilim_dali_pk = bilim_dali_pk;
	}

	public String getMesaj_pk() {
		return mesaj_pk;
	}

	public void setMesaj_pk(String mesaj_pk) {
		this.mesaj_pk = mesaj_pk;
	}

	public String getDergi_pk() {
		return dergi_pk;
	}

	public void setDergi_pk(String dergi_pk) {
		this.dergi_pk = dergi_pk;
	}

	public String getDergi_ismi() {
		return dergi_ismi;
	}

	public void setDergi_ismi(String dergi_ismi) {
		this.dergi_ismi = dergi_ismi;
	}

	public String getEditor_pk() {
		return editor_pk;
	}

	public void setEditor_pk(String editor_pk) {
		this.editor_pk = editor_pk;
	}

	public String getRol() {
		return rol;
	}

	public void setRol(String rol) {
		this.rol = rol;
	}

	public String getSonuc() {
		return sonuc;
	}

	public void setSonuc(String sonuc) {
		this.sonuc = sonuc;
	}

	public String getBasvuru_pk() {
		return basvuru_pk;
	}

	public void setBasvuru_pk(String basvuru_pk) {
		this.basvuru_pk = basvuru_pk;
	}

	public String getUye_pk() {
		return uye_pk;
	}

	public void setUye_pk(String uye_pk) {
		this.uye_pk = uye_pk;
	}

	public String getKarar() {
		return karar;
	}

	public void setKarar(String karar) {
		this.karar = karar;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public int getSayfa() {
		return sayfa;
	}

	public void setSayfa(int sayfa) {
		this.sayfa = sayfa;
	}
	
	private static Object getSession(String key) 
	{
		HttpSession session = ServletActionContext.getRequest().getSession();
		return session.getAttribute(key);
	}
}
